/**
 * 
 * @author JackChu
 * @version 2012-6-13 下午10:50:39
 */
package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import flashx.textLayout.formats.Float;
	
	import jjk.astar.Astar;
	import jjk.model.Grid;
	import jjk.model.Node;
	
	[SWF(width = "480", height = "480")]
	public class AstarDemo extends Sprite
	{
		private var _preGridWidth:int = 10;
		private var _gridCols:int = 48;
		private var _gridRows:int = 48;
		
		//不可通过节点的比例
		private var _uncrossablePrecent:Number = 0.3; 
		
		private var _grid:Grid = null;
		
		private var aStar:Astar=null;
		
		private var _gridBitmap:Bitmap = new Bitmap(new BitmapData(1,1));
		
		private var backSprite:Sprite = new Sprite();
		
		private var pathPlayer:Sprite;
		
		private var path:Sprite;
		
		private var isClick:Boolean=false;
		
		public function AstarDemo()
		{
			stage.frameRate = 120;
			aStar = new Astar();
			aStar.pathType = Astar.TYPE_PATH_FOUR;
			initGrid();
			addChild(backSprite);
			backSprite.addChild(_gridBitmap);
			drawPlayer();
			path = new Sprite();
			
		
			backSprite.addChild(path);
			backSprite.addEventListener(MouseEvent.CLICK,onClick);
			stage.addEventListener(Event.ENTER_FRAME,onEnterFrame);
			aStar.startNode = _grid.getNode(0,0);
		}
		
		protected function onClick(event:Event):void
		{
			var xpos:int = Math.floor(mouseX / _preGridWidth);
			var ypos:int = Math.floor(mouseY / _preGridWidth);
			xpos = Math.min(xpos, _grid.cols - 1);
			ypos = Math.min(ypos, _grid.rows- 1);
			aStar.endNode =_grid.getNode(xpos, ypos);		
			trace("end:",xpos,ypos);
			
			var result:Boolean = aStar.findPath(_grid);
			
			if(result)
			{
				for(var i:int=0;i<aStar.pathNodes.length;i++)
				{
					var node:Node = aStar.pathNodes[i] as Node;
					node.toString();
				}
				isClick = true;
				
				path.graphics.clear();
				path.graphics.lineStyle(0, 0xff0000, 1);
				
				path.graphics.moveTo(pathPlayer.x, pathPlayer.y);
				
				
			}
		}
		
		private function initGrid():void
		{
			madeGrid();	
			
			drawGrid();
			
			//aStar.endNode = _grid.getNode(5,3);
			
			
		
		}
		
		private function drawPlayer():void
		{
			pathPlayer = new Sprite();
			
			pathPlayer.graphics.clear();
			
			pathPlayer.graphics.beginFill(0x0000ff);
			pathPlayer.graphics.drawCircle(0,0,2);
			pathPlayer.graphics.endFill();
			pathPlayer.x = _preGridWidth / 2;
			pathPlayer.y = _preGridWidth / 2;
			backSprite.addChild(pathPlayer);
		}
		
		private function madeGrid():void
		{
			_grid = new Grid(_gridCols,_gridRows);
			for (var i:int = 0; i < _gridCols*_gridRows*_uncrossablePrecent; i++) 
			{
				_grid.setUnCrossable(int(Math.random()*_gridCols),int(Math.random()*_gridRows));
			}
			_grid.setCrossable(0,0);
		}
		
		private function drawGrid():void
		{
			_gridBitmap.bitmapData = new BitmapData(_gridCols*_preGridWidth,_gridRows*_preGridWidth,true,0xffffff);
			
			for (var i:int = 0; i < _gridCols; i++) 
			{
				for (var j:int = 0; j < _gridRows; j++) 
				{
					var node:Node = _grid.getNode(i,j);
					if(node.crossable)
					{
						_gridBitmap.bitmapData.fillRect(new Rectangle(i*_preGridWidth,j*_preGridWidth,_preGridWidth-1,_preGridWidth-1),0xfffef000);
					}
					else
					{
						_gridBitmap.bitmapData.fillRect(new Rectangle(i*_preGridWidth,j*_preGridWidth,_preGridWidth-1,_preGridWidth-1),0xffff0000);
					}
				}
				
			}
			
			
		}
		private var _index:int = 0;
		private function onEnterFrame(event:Event):void {
			if (!isClick){
				return;
			}
			var _path:Array = aStar.pathNodes;
			if(_path == null)
			{
				return;
			}
			var targetX:Number = _path[_index].x * _preGridWidth + _preGridWidth / 2;
			var targetY:Number = _path[_index].y * _preGridWidth + _preGridWidth / 2;
			var dx:Number = targetX - pathPlayer.x;
			var dy:Number = targetY - pathPlayer.y;
			var dist:Number = Math.sqrt(dx * dx + dy * dy);
			if (dist < 1){
				if (_index >= _path.length-1){
					isClick = false;
					aStar.startNode = _grid.getNode( _path[_index].x, _path[_index].y);
					_index = 0;
				}
				else
				{
					_index++;
				}
			} else {
				pathPlayer.x += dx * .5;
				pathPlayer.y += dy * .5;
				trace(pathPlayer.x,pathPlayer.y);
				path.graphics.lineTo(pathPlayer.x, pathPlayer.y);
			}
		}
		
	}
}